<template>
  <div class="questionnaire-fill" v-loading="loading">
    <div v-if="questionnaire" class="questionnaire-content">
      <!-- 问卷标题和描述 -->
      <div class="questionnaire-header">
        <h2 class="title">{{ questionnaire.title }}</h2>
        <div v-if="questionnaire.description" class="description">
          {{ questionnaire.description }}
        </div>
      </div>

      <!-- 问题列表 -->
      <el-form ref="formRef" :model="answers" label-width="0">
        <div v-for="(question, index) in questionnaire.questions" :key="question.id" class="question-item">
          <div class="question-title">
            <span class="question-number">{{ index + 1 }}.</span>
            <span class="question-text">{{ question.title }}</span>
            <span v-if="question.required" class="required">*</span>
          </div>

          <!-- 单选题 -->
          <el-form-item 
            v-if="question.type === 1"
            :prop="`q${question.id}`"
            :rules="question.required ? [{ required: true, message: '请选择答案', trigger: 'change' }] : []"
          >
            <el-radio-group v-model="answers[`q${question.id}`]">
              <el-radio 
                v-for="option in question.options" 
                :key="option.id" 
                :label="option.id"
              >
                {{ option.content }}
              </el-radio>
            </el-radio-group>
          </el-form-item>

          <!-- 多选题 -->
          <el-form-item 
            v-else-if="question.type === 2"
            :prop="`q${question.id}`"
            :rules="question.required ? [{ required: true, message: '请选择答案', trigger: 'change' }] : []"
          >
            <el-checkbox-group v-model="answers[`q${question.id}`]">
              <el-checkbox 
                v-for="option in question.options" 
                :key="option.id" 
                :label="option.id"
              >
                {{ option.content }}
              </el-checkbox>
            </el-checkbox-group>
          </el-form-item>

          <!-- 填空题 -->
          <el-form-item 
            v-else-if="question.type === 3"
            :prop="`q${question.id}`"
            :rules="question.required ? [{ required: true, message: '请填写答案', trigger: 'blur' }] : []"
          >
            <el-input 
              v-model="answers[`q${question.id}`]"
              type="textarea"
              :rows="3"
              placeholder="请输入您的答案"
            />
          </el-form-item>
        </div>
      </el-form>

      <!-- 提交按钮 -->
      <div class="form-actions">
        <el-button type="primary" @click="handleSubmit">提交问卷</el-button>
        <el-button @click="$router.back()">返回</el-button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { getQuestionnaire, submit } from '@/api/questionnaire'
import { useQuestionnaireStore } from '@/stores/questionnaire'

const route = useRoute()
const router = useRouter()
const questionnaireStore = useQuestionnaireStore()

const loading = ref(false)
const questionnaire = ref(null)
const answers = ref({})
const formRef = ref(null)

const loadQuestionnaire = async () => {
  loading.value = true
  try {
    const res = await getQuestionnaire(route.params.id)
    if (res.code === 200) {
      questionnaire.value = res.data
    } else {
      ElMessage.error(res.message || '加载问卷失败')
    }
  } catch (error) {
    console.error('加载问卷失败:', error)
    ElMessage.error('加载问卷失败')
  } finally {
    loading.value = false
  }
}

const handleSubmit = async () => {
  if (!formRef.value) return
  
  try {
    await formRef.value.validate()
    const res = await submit(questionnaire.value.id, answers.value)
    if (res.code === 200) {
      ElMessage.success('提交成功')
      await questionnaireStore.loadSubmittedQuestionnaires()
      router.push('/front/questionnaires')
    } else {
      ElMessage.error(res.message || '提交失败')
    }
  } catch (error) {
    console.error('提交失败:', error)
    ElMessage.error('提交失败')
  }
}

onMounted(() => {
  loadQuestionnaire()
})
</script>

<style lang="scss" scoped>
.questionnaire-fill {
  max-width: 800px;
  margin: 0 auto;
  padding: 20px;
  min-height: 100vh;
  background-color: #ecf5ff;  // 更改为浅蓝色背景
  
  .questionnaire-content {
    background: #ffffff;
    border-radius: 12px;
    padding: 30px;
    box-shadow: 0 4px 16px rgba(64, 158, 255, 0.1);  // 蓝色阴影
    
    .questionnaire-header {
      text-align: center;
      margin-bottom: 30px;
      padding-bottom: 20px;
      border-bottom: 1px solid #d9ecff;  // 浅蓝色分隔线
      
      .title {
        font-size: 24px;
        color: #1e88e5;  // 蓝色标题
        margin-bottom: 10px;
      }
      
      .description {
        color: #64b5f6;  // 浅蓝色描述文字
        font-size: 14px;
        line-height: 1.6;
      }
    }
    
    .question-item {
      margin-bottom: 25px;
      padding: 20px;
      border-radius: 8px;
      background: #f5faff;  // 非常浅的蓝色背景
      border: 1px solid #e1f5fe;
      transition: all 0.3s ease;
      
      &:hover {
        background: #e3f2fd;  // 悬停时的浅蓝色背景
        border-color: #bbdefb;
      }
      
      .question-title {
        margin-bottom: 15px;
        font-size: 16px;
        line-height: 1.5;
        color: #1976d2;  // 深蓝色文字
        font-weight: 500;
        
        .question-number {
          margin-right: 8px;
          color: #2196f3;  // 明亮的蓝色
          font-weight: 600;
        }
        
        .question-text {
          color: #1976d2;  // 深蓝色文字
        }
        
        .required {
          color: #ff4081;  // 粉色必填标记
          margin-left: 4px;
        }
      }
    }
    
    .form-actions {
      text-align: center;
      margin-top: 30px;
      padding-top: 20px;
      border-top: 1px solid #d9ecff;  // 浅蓝色分隔线
      
      .el-button {
        padding: 12px 30px;
        
        &.el-button--primary {
          background-color: #1e88e5;  // 主按钮蓝色
          border-color: #1e88e5;
          
          &:hover {
            background-color: #1976d2;
            border-color: #1976d2;
          }
        }
        
        & + .el-button {
          margin-left: 15px;
        }
      }
    }
  }
}

// 单选和多选选项样式
:deep(.el-radio), :deep(.el-checkbox) {
  margin-right: 30px;
  margin-bottom: 10px;
  padding: 8px 16px;
  border-radius: 4px;
  transition: all 0.3s ease;
  
  &:hover {
    background-color: #e3f2fd;  // 悬停时的浅蓝色背景
  }
  
  .el-radio__label, .el-checkbox__label {
    color: #1976d2;  // 深蓝色文字
  }
  
  &.is-checked {
    .el-radio__label, .el-checkbox__label {
      color: #1e88e5;  // 选中时的蓝色
    }
  }
}

// 填空题输入框样式
:deep(.el-input) {
  width: 100%;
  max-width: 500px;
  
  .el-input__inner, .el-textarea__inner {
    border-color: #bbdefb;  // 浅蓝色边框
    
    &:hover {
      border-color: #64b5f6;
    }
    
    &:focus {
      border-color: #2196f3;
      box-shadow: 0 0 0 2px rgba(33, 150, 243, 0.1);
    }
  }
}

// 错误提示样式
:deep(.el-form-item__error) {
  padding-top: 4px;
  color: #ff4081;  // 粉色错误提示
}
</style> 